package com.cjq.wx.Controller;

import com.cjq.bean.Courier;
import com.cjq.bean.Message;
import com.cjq.bean.User;
import com.cjq.bean.WxUser;
import com.cjq.mvc.ResponseBody;
import com.cjq.service.CourierService;
import com.cjq.service.UserService;
import com.cjq.util.JSONUtil;
import com.cjq.util.RandomUtil;
import com.cjq.util.SMSUtil;
import com.cjq.util.UserUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class UserController {

    @ResponseBody("/wx/loginSms.do")
    public String sendSms(HttpServletRequest req, HttpServletResponse rsp){
        String phone = req.getParameter("phone");
        String code= RandomUtil.getCode() + "";
        boolean flay = SMSUtil.send(phone, code);//生成验证码 并发送个对应的手机号码
        Message msg = new Message();
        if (flay){
            msg.setStatus(0);
            msg.setResult("验证码已发送，请查收");
        }else{
            msg.setStatus(-1);
            msg.setResult("验证码发送错误，请检查手机号或稍后再试");
        }
        UserUtil.setLoginSms(req.getSession(),phone,code);      //将输入的电话：验证  存在session中   电话对应的值是验证码
        return JSONUtil.toJSON(msg);

    }

    //登录或注册   用户的登录或注册    快递员的登录
    @ResponseBody("/wx/login.do")
    public String login(HttpServletRequest req, HttpServletResponse rsp){
        String userPhone = req.getParameter("usrePhone");
        String userCode = req.getParameter("code");

        System.out.println("传过来的电话为："+userPhone+"验证码是："+userCode);

        String sysCode = UserUtil.getLoginSms(req.getSession(),userPhone);
        System.out.println("系统本身存的验证码："+sysCode);
        Message msg = new Message();

        if (userCode==null){
            msg.setStatus(-1);
            msg.setResult("输入的验证码不能为空");
        }else if(userCode.equals(sysCode)){
            //判断是用户还是快递员，如果都没有就是创建用户
            User user = UserService.findByUserPhone(userPhone);
            System.out.println(user);
            Courier courier = CourierService.findBySysPhone(userPhone);
            System.out.println(courier);
            boolean b = false;
            WxUser wxUser = new WxUser();      //这是登录进去之后的放在session的对象
            if (courier!=null){
                req.getSession().setAttribute("adminUserName",courier.getSysName());
                req.getSession().setAttribute("loginPhone",courier.getSysPhone());

                msg.setStatus(1);    //1为快递员登录
                //更改登录时间
                String ip = req.getRemoteAddr();
                b = CourierService.updateLoginTime(userPhone,ip);
                System.out.println("快递员更新登录时间："+b);


                wxUser.setName(courier.getSysName());
                wxUser.setPhone(courier.getSysPhone());
                wxUser.setPassword(courier.getPassword());
                wxUser.setCreateTime(courier.getCreateTime());
                wxUser.setLoginTime(courier.getLoginTime());
                wxUser.setShenfen("快递员");

            }else if (user!=null){
                msg.setStatus(0);       //0为用户登录
                //更改登录时间
                b = UserService.updateLoginTime(userPhone);
                System.out.println("用户更新登录时间："+b);

                wxUser.setName(user.getUserName());
                wxUser.setPhone(user.getUserPhone());
                wxUser.setPassword(user.getPassword());
                wxUser.setCreateTime(user.getCreateTime());
                wxUser.setLoginTime(user.getLoginTime());
                wxUser.setShenfen("用户");

            }else {
                //如果快递员和用户都没有找到该号码，那就在用户表添加一条数据
                msg.setStatus(0);
                b = UserService.insertByPhone(userPhone);
                System.out.println("添加了新的用户："+b);
                wxUser.setPhone(userPhone);
                wxUser.setShenfen("用户");
            }

            UserUtil.setWxUser(req.getSession(),wxUser);




        }else{
            msg.setStatus(-2);
            msg.setResult("验证码不一致，请检查");
        }


        return JSONUtil.toJSON(msg);

    }

    @ResponseBody("/wx/logout.do")
    public String logout(HttpServletRequest req, HttpServletResponse rsp){
        //销毁session
        req.getSession().invalidate();
        //给客户端回信息
        Message msg= new Message();
        msg.setStatus(0);
        return JSONUtil.toJSON(msg);
    }


    //获取用户的身份
    @ResponseBody("/wx/userInfo.do")
    public String userInfo(HttpServletRequest req, HttpServletResponse rsp){
        WxUser wxUser = UserUtil.getWxUser(req.getSession());

        String shenfen = wxUser.getShenfen();

        Message msg = new Message();
        msg.setResult(wxUser.getPhone());
        if ("用户".equals(shenfen)){
            msg.setStatus(0);
        }else if ("快递员".equals(shenfen)){
            msg.setStatus(-1);
        }
        return JSONUtil.toJSON(msg);



    }

}
